<?php
require_once '../classes/ConnectionDB.php';
require_once '../classes/IndirizzoManager.php';
require_once '../classes/ClasseDataManager.php';

$db = ConnectionDB::getInstance()->connectToDB();
$result = IndirizzoManager::getInstance()->getIndirizzi($db);

$codiceClasse = "";
$flag = "";
if (isset($_GET['flag']) && $_GET['flag']==0)
{
    header("Location:../portal/manageErrors.php?lista=30*");
}
?>
<html>
    <head>
        <title>Nuova classe</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <style type="text/css">
            @import url("../css/style.css");
            #bodyBoxCont
            {
                padding-top: 10px;
            }
        </style>
        <script src="../glassboxLatest/glassbox.2.0.0/javascripts/prototype.js" type="text/javascript"></script>
        <script src="../glassboxLatest/glassbox.2.0.0/javascripts/scriptaculous/effects.js" type="text/javascript"></script>
        <script src="../glassboxLatest/glassbox.2.0.0/javascripts/glassbox/glassbox.js" type="text/javascript"></script>

        <script type="text/javascript">
            path_to_root_dir = "../glassboxLatest/glassbox.2.0.0/";
            window.onload = function()
            {
                var headerBox = new GlassBox();
                headerBox.init( 'headerBox', '890px', '300px', 'hidden','', true, false);
                headerBox.ipos();
                headerBox.appear();
                var bodyBox = new GlassBox();
                bodyBox.init( 'bodyBox', '1080px', '680px', 'auto','', true, false);
                bodyBox.ipos();
                bodyBox.appear();
                //                var str = "";
                //                for(i=1;i<=40;i++)
                //                {
                //                    str += '<tr><td>'+i+'° <input type="text" id="nome'+i+'"/></td><td><input type="text" id="cognome'+i+'" /></td><td><input type="text" id="email'+i+'" /></td></tr>';
                //                }
                //                document.getElementById('divStudenti').innerHTML=str;
            }

            function generaUsernamePassword(codice)
            {
                document.getElementById('username'+codice).value=codice;
                document.getElementById('password'+codice).value=generaPassword();
                document.getElementById('statoCredenziali'+codice).innerHTML="Generate";

            }

            function generaPassword()
            {
                N_Caratteri = 10;
                Stringa = "";
                for (I=0;I<N_Caratteri;I++)
                {
                    do
                    {
                        N = Math.floor(Math.random()*74)+48;
                    }
                    while(!(((N >= 48) && (N <= 57)) || ((N >= 65) && (N <= 90)) || ((N >= 97) && (N <= 122))));

                    Stringa = Stringa+String.fromCharCode(N);
                }
                return Stringa;
            }


            function xXMLHttpRequest()
            {

                var client = null;
                if (window.XMLHttpRequest)
                {
                    client=new XMLHttpRequest();
                }
                // code for IE
                else if (window.ActiveXObject)
                {
                    client=new ActiveXObject("Microsoft.XMLHTTP");
                }

                return client;
            }


            function handler() {
                if(this.readyState == 4 && this.status == 200)
                {
                    var num_option=document.getElementById('indirizzoSelect').options.length;
                    var tmp = this.responseText.split("*");

                    document.getElementById('indirizzoSelect').options[num_option]=new Option('',escape(tmp[0]),false,false);
                    document.getElementById('indirizzoSelect').options[num_option].innerHTML = tmp[1];


                } else if (this.readyState == 4 && this.status != 200) {
                    // fetched the wrong page or network error...
                    alert("error this.status : " + this.status );

                }
            }

            function newIndirizzo()
            {
                var client = xXMLHttpRequest();
                client.onreadystatechange = handler;

                var indi = window.prompt("Indirizzo","Inserisci il nuovo indirizzo");
                if(indi!=null && indi!="Inserisci il nuovo indirizzo")
                {
                    var param = "descrizioneIndirizzo="+ indi;

                    client.open("POST", "nuovoIndirizzo.php", true);
                    client.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

                    client.send(param);
                }

            }




            function salvaClasse()
            {


                document.getElementById('classeValido').value=document.getElementById('classeSelect').value;
                document.getElementById('sezioneValido').value=document.getElementById('sezioneSelect').value;
                document.getElementById('indirizzoValido').value=document.getElementById('indirizzoSelect').value;
                var str="";
                for(u=1;u<=40;u++)
                {
                    if(document.getElementById('nome'+u).value!="" && document.getElementById('cognome'+u).value!="" && document.getElementById('username'+u).value!="" && document.getElementById('password'+u).value!="")
                    {
                        //                        alert("str = " + str);
                        str += document.getElementById('nome'+u).value+"*"+document.getElementById('cognome'+u).value+"*"+document.getElementById('email'+u).value+"*"+document.getElementById('username'+u).value+"*"+document.getElementById('password'+u).value+"+";
                    }
                }

                document.getElementById('strValido').value=str;

                document.getElementById('formStudente').submit();
            }


        </script>
    </head>
    <body>

        <?
        if ($flag != "" && $flag == '0')
        {
            ?>
        <center>Attenzione classe già inserita. Clicca <a href="nuovaClasse.php">qui</a> per inserirne una nuova.</center>
            <?
        }
        else
        {
            ?>

        <form method="POST" name="formIndi" action="nuovoIndirizzo.php">
            <input type="hidden" name="descrizioneIndirizzo" id="descrizioneIndirizzo" />
        </form>
            <?php include '../portal/header.php'; ?>
        <center>
            <div id="divCredenziali"></div>
            <script>
            var t=0;
            var str="";
            for(t=1;t<=40;t++)
            {
                str+=' <input type="hidden" id="username'+t+'"  /><input type="hidden" id="password'+t+'" />';
            }
            document.getElementById('divCredenziali').innerHTML=str;

        </script>
            <div id="headerBox">
                <table cellpadding="20" >
                    <tr>
                        <td colspan="3" style="color: red; text-align: right"><h3>I campi contrassegnati con "*" sono obbligatori</h3></td>
                    </tr>
                    <tr>
                        <th>Classe*</th>
                        <td>
                            <select name="classe" id="classeSelect">
                                <option value="1">1</option>
                                <option value="2">2</option>
                                <option value="3">3</option>
                                <option value="4">4</option>
                                <option value="5">5</option>
                            </select>
                        </td>

                        <th>Sezione*</th>
                        <td><input type="text" name="sezione" id="sezioneSelect" size="2"/></td>

                        <th>Indirizzo*</th>
                        <td>
                            <select name="indirizzo" id="indirizzoSelect">
                                    <?php
                                    while ($row = $result->fetch_row())
                                    {
                                        echo '<option value=' . $row[0] . '>' . stripcslashes($row[1]) . '</option>';
                                    }
                                    ?>
                            </select>

                        </td>
                    </tr>
                    <tr>
                        <th colspan="9"><a href="javascript:newIndirizzo();">Nuovo indirizzo</a></th>
                    </tr>
                </table>
            </div>

            <div id="bodyBox">
                <div id="bodyBoxCont">
                    <form id="formStudente" method="POST" action="nuovoStudenteListener.php">

                        <input type="hidden" name="classe" id ="classeValido"/>
                        <input type="hidden" name="sezione" id ="sezioneValido"/>
                        <input type="hidden" name="indirizzo" id ="indirizzoValido"/>

                        <input type="hidden" name="strValido" id ="strValido"/>

                        <table id="tableStudents" >
                            <tr>
                                <th style="color: #432db1 ">Nome*</th>
                                <th style="color: #432db1 ">Cognome*</th>
                                <th style="color: #432db1 ">Email</th>
                                <th style="color: #432db1 ">Stato credenziali</th>
                                <th style="color: #432db1 ">Genera credenziali</th>
                            </tr>
                            <tr><td>&nbsp;</td>
                            </tr>
                            <input type="hidden" id="codiceClasse" name="codiceClasse" value="<?= $codiceClasse ?>"/>

                            
                            <tr>
                                <td>1° <input type="text" id="nome1"/></td>
                                <td><input type="text" id="cognome1" /></td>
                                <td><input type="text" id="email1" /></td>
                                <td><div id="statoCredenziali1" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(1);" />
                            </tr>
                            <tr>
                                <td>2° <input type="text" id="nome2"/></td>
                                <td><input type="text" id="cognome2" /></td>
                                <td><input type="text" id="email2" /></td>
                                <td><div id="statoCredenziali2" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(2);" />
                           </tr>
                            <tr>
                                <td>3° <input type="text" id="nome3"/></td>
                                <td><input type="text" id="cognome3" /></td>
                                <td><input type="text" id="email3" /></td>
                                <td><div id="statoCredenziali3" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(3);" />
                            </tr>
                            <tr>
                                <td>4° <input type="text" id="nome4"/></td>
                                <td><input type="text" id="cognome4" /></td>
                                <td><input type="text" id="email4" /></td>
                                <td><div id="statoCredenziali4" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(4);" />
                                </tr>
                            <tr>
                                <td>5° <input type="text" id="nome5"/></td>
                                <td><input type="text" id="cognome5" /></td>
                                <td><input type="text" id="email5" /></td>
                                <td><div id="statoCredenziali5" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(5);" />
                            </tr>
                            <tr>
                                <td>6° <input type="text" id="nome6"/></td>
                                <td><input type="text" id="cognome6" /></td>
                                <td><input type="text" id="email6" /></td>
                                <td><div id="statoCredenziali6" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(6);" />
                            </tr>
                            <tr>
                                <td>7° <input type="text" id="nome7"/></td>
                                <td><input type="text" id="cognome7" /></td>
                                <td><input type="text" id="email7" /></td>
                                <td><div id="statoCredenziali7" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(7);" />
                            </tr>
                            <tr>
                                <td>8° <input type="text" id="nome8"/></td>
                                <td><input type="text" id="cognome8" /></td>
                                <td><input type="text" id="email8" /></td>
                                <td><div id="statoCredenziali8" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(8);" />
                            </tr>
                            <tr>
                                <td>9° <input type="text" id="nome9"/></td>
                                <td><input type="text" id="cognome9" /></td>
                                <td><input type="text" id="email9" /></td>
                                <td><div id="statoCredenziali9" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(9);" />
                            </tr>
                            <tr>
                                <td>10°<input type="text" id="nome10"/></td>
                                <td><input type="text" id="cognome10" /></td>
                                <td><input type="text" id="email10" /></td>
                                <td><div id="statoCredenziali10" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(10);" />
                            </tr>

                            <tr>
                                <td>11°<input type="text" id="nome11"/></td>
                                <td><input type="text" id="cognome11" /></td>
                                <td><input type="text" id="email11" /></td>
                                <td><div id="statoCredenziali11" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(11);" />
                            </tr>
                            <tr>
                                <td>12°<input type="text" id="nome12"/></td>
                                <td><input type="text" id="cognome12" /></td>
                                <td><input type="text" id="email12" /></td>
                                <td><div id="statoCredenziali12" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(12);" />
                            </tr>
                            <tr>
                                <td>13°<input type="text" id="nome13"/></td>
                                <td><input type="text" id="cognome13" /></td>
                                <td><input type="text" id="email13" /></td>
                                <td><div id="statoCredenziali13" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(13);" />
                            </tr>
                            <tr>
                                <td>14°<input type="text" id="nome14"/></td>
                                <td><input type="text" id="cognome14" /></td>
                                <td><input type="text" id="email14" /></td>
                                <td><div id="statoCredenziali14" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(14);" />
                            </tr>
                            <tr>
                                <td>15°<input type="text" id="nome15"/></td>
                                <td><input type="text" id="cognome15" /></td>
                                <td><input type="text" id="email15" /></td>
                                <td><div id="statoCredenziali15" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(15);" />
                            </tr>
                            <tr>
                                <td>16°<input type="text" id="nome16"/></td>
                                <td><input type="text" id="cognome16" /></td>
                                <td><input type="text" id="email16" /></td>
                                <td><div id="statoCredenziali16" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(16);" />
                            </tr>
                            <tr>
                                <td>17°<input type="text" id="nome17"/></td>
                                <td><input type="text" id="cognome17" /></td>
                                <td><input type="text" id="email17" /></td>
                                <td><div id="statoCredenziali17" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(17);" />
                            </tr>
                            <tr>
                                <td>18°<input type="text" id="nome18"/></td>
                                <td><input type="text" id="cognome18" /></td>
                                <td><input type="text" id="email18" /></td>
                                <td><div id="statoCredenziali18" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(18);" />
                            </tr>
                            <tr>
                                <td>19°<input type="text" id="nome19"/></td>
                                <td><input type="text" id="cognome19" /></td>
                                <td><input type="text" id="email19" /></td>
                                <td><div id="statoCredenziali19" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(19);" />
                            </tr>
                            <tr>
                                <td>20°<input type="text" id="nome20"/></td>
                                <td><input type="text" id="cognome20" /></td>
                                <td><input type="text" id="email20" /></td>
                                <td><div id="statoCredenziali20" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(20);" />
                            </tr>
                            <tr>
                                <td>21°<input type="text" id="nome21"/></td>
                                <td><input type="text" id="cognome21" /></td>
                                <td><input type="text" id="email21" /></td>
                                <td><div id="statoCredenziali21" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(21);" />
                            </tr>
                            <tr>
                                <td>22°<input type="text" id="nome22"/></td>
                                <td><input type="text" id="cognome22" /></td>
                                <td><input type="text" id="email22" /></td>
                                <td><div id="statoCredenziali22" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(22);" />
                            </tr>
                            <tr>
                                <td>23°<input type="text" id="nome23"/></td>
                                <td><input type="text" id="cognome23" /></td>
                                <td><input type="text" id="email23" /></td>
                                <td><div id="statoCredenziali23" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(23);" />
                            </tr>
                            <tr>
                                <td>24°<input type="text" id="nome24"/></td>
                                <td><input type="text" id="cognome24" /></td>
                                <td><input type="text" id="email24" /></td>
                                <td><div id="statoCredenziali24" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(24);" />
                            </tr>
                            <tr>
                                <td>25°<input type="text" id="nome25"/></td>
                                <td><input type="text" id="cognome25" /></td>
                                <td><input type="text" id="email25" /></td>
                                <td><div id="statoCredenziali25" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(25);" />
                            </tr>
                            <tr>
                                <td>26°<input type="text" id="nome26"/></td>
                                <td><input type="text" id="cognome26" /></td>
                                <td><input type="text" id="email26" /></td>
                                <td><div id="statoCredenziali26" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(26);" />
                            </tr>
                            <tr>
                                <td>27°<input type="text" id="nome27"/></td>
                                <td><input type="text" id="cognome27" /></td>
                                <td><input type="text" id="email27" /></td>
                                <td><div id="statoCredenziali27" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(27);" />
                            </tr>
                            <tr>
                                <td>28°<input type="text" id="nome28"/></td>
                                <td><input type="text" id="cognome28" /></td>
                                <td><input type="text" id="email28" /></td>
                                <td><div id="statoCredenziali28" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(28);" />
                            </tr>
                            <tr>
                                <td>29°<input type="text" id="nome29"/></td>
                                <td><input type="text" id="cognome29" /></td>
                                <td><input type="text" id="email29" /></td>
                                <td><div id="statoCredenziali29" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(29);" />
                            </tr>
                            <tr>
                                <td>30°<input type="text" id="nome30"/></td>
                                <td><input type="text" id="cognome30" /></td>
                                <td><input type="text" id="email30" /></td>
                                <td><div id="statoCredenziali30" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(30);" />
                            </tr>

                            <tr>
                                <td>31°<input type="text" id="nome31"/></td>
                                <td><input type="text" id="cognome31" /></td>
                                <td><input type="text" id="email31" /></td>
                                <td><div id="statoCredenziali31" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(31);" />
                            </tr>
                            <tr>
                                <td>32°<input type="text" id="nome32"/></td>
                                <td><input type="text" id="cognome32" /></td>
                                <td><input type="text" id="email32" /></td>
                                <td><div id="statoCredenziali32" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(32);" />
                            </tr>
                            <tr>
                                <td>33°<input type="text" id="nome33"/></td>
                                <td><input type="text" id="cognome33" /></td>
                                <td><input type="text" id="email33" /></td>
                                <td><div id="statoCredenziali33" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(33);" />
                            </tr>
                            <tr>
                                <td>34°<input type="text" id="nome34"/></td>
                                <td><input type="text" id="cognome34" /></td>
                                <td><input type="text" id="email34" /></td>
                                <td><div id="statoCredenziali34" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(34);" />
                            </tr>
                            <tr>
                                <td>35°<input type="text" id="nome35"/></td>
                                <td><input type="text" id="cognome35" /></td>
                                <td><input type="text" id="email35" /></td>
                                <td><div id="statoCredenziali35" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(35);" />
                            </tr>
                            <tr>
                                <td>36°<input type="text" id="nome36"/></td>
                                <td><input type="text" id="cognome36" /></td>
                                <td><input type="text" id="email36" /></td>
                                <td><div id="statoCredenziali36" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(36);" />
                            </tr>
                            <tr>
                                <td>37°<input type="text" id="nome37"/></td>
                                <td><input type="text" id="cognome37" /></td>
                                <td><input type="text" id="email37" /></td>
                                <td><div id="statoCredenziali37" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(37);" />
                            </tr>
                            <tr>
                                <td>38°<input type="text" id="nome38"/></td>
                                <td><input type="text" id="cognome38" /></td>
                                <td><input type="text" id="email38" /></td>
                                <td><div id="statoCredenziali38" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(38);" />
                            </tr>
                            <tr>
                                <td>39°<input type="text" id="nome39"/></td>
                                <td><input type="text" id="cognome39" /></td>
                                <td><input type="text" id="email39" /></td>
                                <td><div id="statoCredenziali39" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(39);" />
                            </tr>
                            <tr>
                                <td>40°<input type="text" id="nome40"/></td>
                                <td><input type="text" id="cognome40" /></td>
                                <td><input type="text" id="email40" /></td>
                                <td><div id="statoCredenziali40" style="color: green;text-align: center" ></div></td>
                                <td><input type="button" value="Genera credenziali" onclick="javascript:generaUsernamePassword(40);" />
                            </tr>

                            <tr>
                                <td colspan="2">&nbsp;</td>
                                <td>
                                    <input type="button" value="Inserisci" onclick="salvaClasse()"/>
                                </td>
                            </tr>

                        </table>
                    </form>
                </div>

            </div>
        </center>
            <? } ?>
    </body>

</html>